package com.dmap.knowledge.service.impl;

import com.dmap.knowledge.controller.api.param.ArticleHistoryParam;
import com.dmap.knowledge.controller.api.param.BannerHistoryParam;
import com.dmap.knowledge.dao.BannerBrowseHistoryMapper;
import com.dmap.knowledge.po.Article;
import com.dmap.knowledge.po.Banner;
import com.dmap.knowledge.po.BannerBrowseHistory;
import com.dmap.knowledge.po.BannerBrowseHistoryExample;
import com.dmap.knowledge.service.*;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author zmh
 * @date 18-7-17 下午5:40
 */

@Service
public class BannerHistoryServiceImpl implements BannerHistoryService {
    @Resource
    private BannerBrowseHistoryMapper bannerBrowseHistoryMapper;
    @Resource
    private ArticleService articleService;
    @Resource
    private BannerService bannerService;
    @Resource
    private ArticleHistoryService articleHistoryService;
    @Resource
    private BannerExtensionService bannerExtensionService;

    @Override
    public int insertBannerHistory(ArticleHistoryParam param) {
        BannerBrowseHistory bannerBrowseHistory = new BannerBrowseHistory();
        //计数
        int count = 0;
        List<Banner> bannerList = bannerExtensionService.getBannerByJumpId(Long.parseLong(param.getArticleId()));
        if (CollectionUtils.isNotEmpty(bannerList)) {
            for (Banner banner : bannerList) {
                bannerBrowseHistory.setBannerId(banner.getId());
                bannerBrowseHistory.setBrowseTime(System.currentTimeMillis() / 1000);
                if (param.getUserId() != null) {
                    bannerBrowseHistory.setUserId(Long.parseLong(param.getUserId()));
                }
                Article article = articleService.selectByArticleId(Long.parseLong(param.getArticleId()));
                if (article == null) {
                    return 0;
                }
                count += bannerBrowseHistoryMapper.insertSelective(bannerBrowseHistory);
            }
            if (count == bannerList.size()) {
                return 1;
            } else {
                return 0;
            }
        } else {
            return 1;
        }
    }
    @Override
    public int insertBannerHistory(BannerHistoryParam param) {
        if(countBannerHistory(param)>0){
            deleteBannerHistory(param);
        }
        BannerBrowseHistory bannerBrowseHistory = new BannerBrowseHistory();
        bannerBrowseHistory.setUserId(Long.parseLong(param.getUserId()));
        bannerBrowseHistory.setBannerId(Long.parseLong(param.getBannerId()));
        bannerBrowseHistory.setBrowseTime(System.currentTimeMillis() / 1000);
        int bannerSuccess = bannerBrowseHistoryMapper.insertSelective(bannerBrowseHistory);
        if (bannerSuccess == 0) {
            return 0;
        }
        bannerService.updateBannerBrowseNumber(bannerBrowseHistory.getBannerId());
        //增加article的信息
        //如果不存在就返回0
        Article article = articleService.selectByArticleId(Long.parseLong(param.getJumpId()));
        if (article == null) {
            return 0;
        }
        ArticleHistoryParam articleHistoryParam = new ArticleHistoryParam();
        articleHistoryParam.setArticleId(param.getJumpId());
        articleHistoryParam.setUserId(param.getUserId());
        return articleHistoryService.insertArticle(articleHistoryParam);
    }
    
    @Override
    public int countBannerHistory(BannerHistoryParam bannerHistoryParam) {
        BannerBrowseHistoryExample bannerBrowseHistoryExample = new BannerBrowseHistoryExample();
        BannerBrowseHistoryExample.Criteria criteria = bannerBrowseHistoryExample.createCriteria();
        criteria.andUserIdEqualTo(Long.parseLong(bannerHistoryParam.getUserId()));
        criteria.andBannerIdEqualTo(Long.parseLong(bannerHistoryParam.getBannerId()));
        return bannerBrowseHistoryMapper.countByExample(bannerBrowseHistoryExample);
    }
    
    @Override
    public int deleteBannerHistory(BannerHistoryParam bannerHistoryParam) {
        BannerBrowseHistoryExample bannerBrowseHistoryExample = new BannerBrowseHistoryExample();
        BannerBrowseHistoryExample.Criteria criteria = bannerBrowseHistoryExample.createCriteria();
        criteria.andUserIdEqualTo(Long.parseLong(bannerHistoryParam.getUserId()));
        criteria.andBannerIdEqualTo(Long.parseLong(bannerHistoryParam.getBannerId()));
        return bannerBrowseHistoryMapper.deleteByExample(bannerBrowseHistoryExample);
    
    }
}
